package com.ws.controller;

import com.ws.common.PaasPage;
import com.ws.common.PaasResponse;
import com.ws.entity.Reader;
import com.ws.response.ReaderListResponse;
import com.ws.response.ReaderPageResponse;
import com.ws.response.ReaderResponse;
import com.ws.service.ReaderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author ：ws
 * @date ：Created in 2023/10/24 12:06
 * @description：TODO
 * @modified By：
 * @version: 1.0.0
 */
@Controller
@RefreshScope
@CrossOrigin
@RequestMapping("/reader")
public class ReaderController {
    @Autowired
    ReaderService readerService;

    /**
     * 读者查询接口
     * @param pageable
     * @param readerName
     * @param gender
     * @return
     */
    @GetMapping("")
    public ResponseEntity<ReaderPageResponse> getReaderPage(
            @PageableDefault(page = 1, direction = Sort.Direction.DESC) Pageable pageable,
            @RequestParam(value="readerName",required=false) String readerName,
            @RequestParam(value="gender",required=false) String gender,
            @RequestParam(value="statusCd",required=false) String statusCd
    ){
        PaasPage<Reader> readerPaasPage = readerService.getReaderPage(pageable,readerName,gender,statusCd);
        return ResponseEntity.ok(new ReaderPageResponse(readerPaasPage));
    }

    /**
     * 读者查询接口
     * @param readerName
     * @param gender
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<ReaderListResponse> getReaderList(
            @RequestParam(value="readerName",required=false) String readerName,
            @RequestParam(value="gender",required=false) String gender,
            @RequestParam(value="statusCd",required=false) String statusCd
    ){
        List<Reader> readerList = readerService.getReaderList(readerName,gender,statusCd);
        return ResponseEntity.ok(new ReaderListResponse(readerList));
    }

    /**
     * 读者新增接口
     * @param reader
     * @return
     */
    @PostMapping("")
    public ResponseEntity<ReaderResponse> saveReader(@RequestBody @Valid Reader reader){
        Reader reader1 = readerService.saveReader(reader);
        return ResponseEntity.ok(new ReaderResponse(reader1));
    }

    /**
     * 查询对应读者的基本信息
     * @param readerId
     * @return
     */
    @GetMapping("/{readerId}/basicInfo")
    public ResponseEntity<ReaderResponse> getReaderBasicInfo(@PathVariable Long readerId){
        Reader reader = readerService.getReaderBasicInfo(readerId);
        return ResponseEntity.ok(new ReaderResponse(reader));
    }

    /**
     * 删除对应的员工
     * @param readerId
     * @return
     */
    @PostMapping("/d/{readerId}")
    public ResponseEntity delReader(@PathVariable Long readerId)  {
        readerService.delReader(readerId);
        return ResponseEntity.ok(new PaasResponse());
    }

    /**
     * 修改对应的读者
     * @param readerId
     * @return
     */
    @PostMapping("/u/{readerId}")
    public ResponseEntity<ReaderResponse> updateReader(@PathVariable Long readerId,@RequestBody Reader reader)  {
        Reader reader1 = readerService.updateReader(readerId,reader);
        return ResponseEntity.ok(new ReaderResponse(reader1));
    }
}
